public class HashBuck {
    static class Node{
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    Node[] array=new Node[10];
    public int usedSize;

    //查找
    public int getVal(int key){
        int index=key%array.length;
        Node cur=array[index];
        while (cur!=null){
            if(cur.key==key){
                return cur.val;
            }
            cur=cur.next;
        }
        //没有找到
        return -1;
    }
    //重置数组大小
    public void resize(){
        Node[] newArray=new Node[2*array.length];
        for (int i = 0; i <array.length; i++) {
            Node pcur=array[i];
            while (pcur!=null){
                int index=pcur.key%newArray.length;
                Node pcurN=pcur.next;
                pcur.next=newArray[index];
                newArray[index]=pcur;
                pcur=pcurN;
            }
        }
        array=newArray;
    }

    //负载因子
    public double doLoadFator(){
        return usedSize*1.0/array.length;
    }

    //插入
    public void push(int key,int val){
        int index=key%array.length;
        Node newnode=new Node(key,val);
        Node cur=array[index];
        while (cur!=null){
            if(cur.key==key){
                cur.val=val;
                return;
            }
            cur=cur.next;
        }
        newnode.next=array[index];
        array[index]=newnode;
        usedSize++;
        if(doLoadFator()>=0.75){
            resize();
        }
    }

}
